public class Solution {
    class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode head;

    public ListNode reverseList(ListNode head) {
        ListNode cur = null;
        ListNode prev = head;
        while(head != null){
            head = head.next;
            prev.next = cur;
            cur = prev;
            prev = head;
        }
        return cur;
    }

    public ListNode middleNode(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    public ListNode FindKthToTail(ListNode head, int k) {
        if(head == null){
            return null;
        }

        if(k == 0)
            return null;

        ListNode slow = head;
        ListNode fast = head;

        int length = 0;

        ListNode tmp = head;
        while(head != null){
            head = head.next;
            length++;
        }

        if(length < k){
            return null;
        }

        int count = k;

        while (fast != null && count > 0) {
            fast = fast.next;
            count--;
        }

        while (fast != null) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
}
